import wxValidate from 'wechat-validate'
Page({
  behaviors: [wxValidate],
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    appointment: '',
    dateLayerVisible: false,
    houseList: [],
    houseId: '',
    houseName: '',
    repairItem: [],
    repairItemId: '',
    repairItemName: '',
    mobile: '',
    description: '',
    attachment: [],
  },
  rules: {
    houseId: [{ required: true, message: '请选择报修房屋!' }],
    repairItemId: [{ required: true, message: '请选择维修的项目!' }],
    mobile: [
      { required: true, message: '请填写手机号码!' },
      { pattern: /^1[3-9]\d{9}$/, message: '请填写正确的手机号码!' },
    ],
    appointment: [{ required: true, message: '请选择预约日期!' }],
    description: [{ required: true, message: '请填写问题描述!' }],
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  goList() {
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })
  },
  onLoad(){
    //获取房屋列表
    this.getHouseList()
     // 获取维修项目
     this.getRepairItem()
  },

  //练习代码
  // 获取房屋列表（必须是通过审核的房屋）
  async getHouseList() {
    // 调用接口
    const houseList = await wx.http.get('/house')
    // 渲染数据
    this.setData({ houseList })
  },
  // 获取维修项目
  async getRepairItem(){
    const repairItem = await wx.http.get('/repairItem')
    // 渲染数据
    this.setData({ repairItem })
  },
  //选择房屋信息
  selectHouseInfo({detail:{id,name}}){
    this.setData({
      houseId:id,
      houseName:name
    })
  },
  //选中维修项目回调
  selectRepairItemInfo({detail:{id,name}}){
    this.setData({
      repairItemId: id,
      repairItemName: name,
    })
  },
  //选择预约日期的回调
  selectDateInfo(ev){
    this.setData({
      appointment: wx.utils.dataFormat(ev.detail),
      dateLayerVisible: false,
    })
  },
  //选择上传图片的回调
  async uploadPicture(ev){
    // 上传文件的信息
    const { file } = ev.detail
    const res = await wx.http.upload('/upload',{
      name:'file',
      filePath:file.url
    })
     // 渲染数据，展开原本的数组，再追加后面的数据
     this.setData({ attachment:[...this.data.attachment,res] })
  },
  //删除图片
  uploadDelete(ev){
    this.setData({
      attachment: this.data.attachment.filter((v,index) => index !== ev.detail.index)
    })
  },
  //上传图片之前
  beforeRead(ev){
    const { file,callback} = ev.detail
    //如果图片大于1M，就拦截，不允许上传
    if(file.size > 1024 * 1024){
      wx.utils.toast('文件大小不能超过1MB')
      return callback(false)
    }
    callback(true)
  },
  //提交报修
  async submitForm(){
    // 验证表单数据
    if (!this.validate()) return
    await wx.http.post('/repair',{...this.data})
     // 跳转到报修列表页面
     wx.redirectTo({
      url: '/repair_pkg/pages/list/index',
    })
  }
})
